package com.shinbada.modules.competitor.web;

import com.shinbada.excel.modle.ImportExcelResult;
import com.shinbada.excel.modle.UploadModle;
import com.shinbada.modules.competitor.entity.DTO.CompetitorControlDTO;
import com.shinbada.modules.competitor.entity.VO.CompetitorControlResultVO;
import com.shinbada.modules.competitor.entity.VO.CompetitorDataItemVO;
import com.shinbada.modules.competitor.entity.VO.CompetitorNoumenonResultVO;
import com.shinbada.modules.competitor.entity.VO.CompetitorProductDataItemVO;
import com.shinbada.modules.competitor.utils.TreeUtilCommon;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.ConstraintViolationException;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import com.google.common.collect.Lists;
import com.shinbada.common.utils.DateUtils;
import com.shinbada.common.json.AjaxJson;
import com.shinbada.core.persistence.Page;
import com.shinbada.core.web.BaseController;
import com.shinbada.common.utils.StringUtils;
import com.shinbada.modules.competitor.entity.CompetitorControl;
import com.shinbada.modules.competitor.service.CompetitorControlService;

import com.shinbada.excel.ExcelKit;

/**
 * 控制器Controller
 * @author initcap
 * @version 2021-08-05
 */
@RestController
@RequestMapping(value = "/competitor/competitorControl")
@Api(tags = "控制器")
public class CompetitorControlController extends BaseController {

	@Autowired
	private CompetitorControlService competitorControlService;

	@ModelAttribute
	public CompetitorControl get(@RequestParam(required=false) String id) {
		CompetitorControl entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = competitorControlService.get(id);
		}
		if (entity == null){
			entity = new CompetitorControl();
		}
		return entity;
	}

	@RequiresPermissions("user")
	@GetMapping("list")
	@ApiOperation("控制器列表数据")
	public AjaxJson list(CompetitorControl competitorControl, HttpServletRequest request, HttpServletResponse response) {
		Page<CompetitorControl> page = competitorControlService.findPage(new Page<>(request, response), competitorControl);
		return AjaxJson.success().put("page",page);
	}

	@RequiresPermissions("user")
	@GetMapping("queryById")
	@ApiOperation("根据Id获取控制器数据")
	public AjaxJson queryById(CompetitorControl competitorControl) {
		return AjaxJson.success().put("competitorControl", competitorControl);
	}

	@RequiresPermissions(value={"competitor:competitorControl:add","competitor:competitorControl:edit"},logical=Logical.OR)
	@PostMapping("save")
	@ApiOperation("保存控制器")
	public AjaxJson save(CompetitorControl competitorControl) {
		/**
		 * 后台hibernate-validation插件校验
		 */
		String errMsg = beanValidator(competitorControl);
		if (StringUtils.isNotBlank(errMsg)){
			return AjaxJson.error(errMsg);
		}
		//新增或编辑表单保存
		competitorControlService.save(competitorControl);
		return AjaxJson.success("保存控制器成功");
	}

	@RequiresPermissions("competitor:competitorControl:del")
	@DeleteMapping("delete")
	@ApiOperation("批量删除控制器")
	public AjaxJson delete(String ids) {
		String[] idArray =ids.split(",");
		for(String id : idArray){
			competitorControlService.delete(competitorControlService.get(id));
		}
		return AjaxJson.success("删除控制器成功");
	}

	@RequiresPermissions("competitor:competitorControl:export")
	@GetMapping("export")
	@ApiOperation("导出控制器")
	public void export(CompetitorControl entity, HttpServletResponse response) {
		ExcelKit.$Export(CompetitorControl.class, response).downXlsx(competitorControlService.findList(entity), false);
	}

	@RequiresPermissions("competitor:competitorControl:import")
	@GetMapping("template")
	@ApiOperation("导出控制器模版")
	public void export(HttpServletResponse response) {
		ExcelKit.$Export(CompetitorControl.class, response).downXlsx(new ArrayList<>(), true);
	}

	@RequiresPermissions("competitor:competitorControl:import")
	@PostMapping(value = "import")
	@ApiImplicitParam(name = "modle", paramType = "query", allowableValues = "skip,exit,cover")
	@ApiOperation("导入文件,下载模版为/export")
	public AjaxJson uploadExcel(MultipartFile file, UploadModle modle) {
		if (shinProperites.isDemoMode()) {
			return AjaxJson.error("演示模式，不允许操作！");
		}
		ImportExcelResult<CompetitorControl> result = competitorControlService.uploadExcel(file, modle);
		return AjaxJson.success(result);
	}
	@RequiresPermissions("user")
	@GetMapping("getControlTreeStructure")
	@ApiOperation("根据筛选条件获取控制器树结构")
	public AjaxJson getControlTreeStructure(CompetitorControlDTO competitorControlDTO) throws Exception {
		List<CompetitorControlResultVO> competitorControlResultVOList  = competitorControlService.getControlTreeStructure(competitorControlDTO);
		for (CompetitorControlResultVO competitorControlResultVO : competitorControlResultVOList) {
			Map<String, CompetitorDataItemVO> map = competitorControlResultVO.getCompetitorDataItemVOS().stream()
					.collect(Collectors.toMap(CompetitorDataItemVO::getDataItemId, o -> o, (o, o1) -> o));
			competitorControlResultVO.setCompetitorDataItemVOMap(map);
			competitorControlResultVO.setCompetitorDataItemVOS(new ArrayList<>());
		}
		return AjaxJson.success().put("data",competitorControlResultVOList);
	}
	@RequiresPermissions("user")
	@GetMapping("getControlByNoumenonId/{noumenonId}")
	@ApiOperation("根据本体获取该本体下所有控制器")
	public AjaxJson getControlByNoumenonId(@PathVariable String noumenonId) {
		List<CompetitorControl>competitorControlList = competitorControlService.getControlByNoumenonId(noumenonId);
		return AjaxJson.success().put("competitorControl", competitorControlList);
	}
	@RequiresPermissions("user")
	@GetMapping("getControlByNoumenonModel")
	@ApiOperation("根据本体型号获取该本体下所有控制器")
	public AjaxJson getControlByNoumenonModel(String model) {
		List<CompetitorControl>competitorControlList = competitorControlService.getControlByNoumenonModel(model);
		return AjaxJson.success().put("data", competitorControlList);
	}
}
